package com.spring.boot.config;

import org.quartz.Scheduler;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

/**
 * @program: springboot_vue
 * @description: Job定时配置
 * @author: ry
 * @create: 2018-08-16 13:44
 **/
@Configuration//代替xml文件
@EnableScheduling //开启对定时任务支持
public class SchedulerConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public JobFactory jobFactory(){
        JobBeanJobFactory jobFactory=new JobBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }

    /**
     * 初始化schedulerfatory
     * @return
     * @throws IOException
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean()throws IOException{
        SchedulerFactoryBean factory=new SchedulerFactoryBean();
        factory.setAutoStartup(true);
        factory.setJobFactory(jobFactory());
        factory.setStartupDelay(3000);
        factory.setQuartzProperties(quartzProperties());
        return factory;
    }

    /**
     * 加载quartz
     * @return
     * @throws IOException
     */
    @Bean
    public Properties quartzProperties()throws IOException{
        PropertiesFactoryBean propertiesFactoryBean=new PropertiesFactoryBean();
        File file=new File("quartz.properties");//从jar同级目录加载
        if(!file.exists()){//去config目录找
            file=new File("config/quartz.properties");
        }
        Resource resource=new FileSystemResource(file);
        if(!resource.exists()){//从config目录下找不到,直接使用classpath目录下
            resource=new ClassPathResource("/quartz.properties");
        }
        propertiesFactoryBean.setLocation(resource);
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    /**
     * 伴随spring启动
     * @param contextRefreshedEvent
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            try {
                Scheduler sc=schedulerFactoryBean().getScheduler();
                sc.start();
                System.out.println("定时任务启动成功.....");
            }catch (Exception e){
                e.printStackTrace();
            }
    }
}
